package com.cjh.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjh.common.dto.*;
import com.cjh.common.entity.Commodity;
import com.cjh.common.entity.GroupValue;
import com.cjh.common.entity.Specifications;
import com.cjh.common.entity.TypeGroup;
import com.cjh.common.mapper.CommodityMapper;
import com.cjh.common.mapper.GroupValueMapper;
import com.cjh.common.mapper.TypeGroupMapper;
import com.cjh.common.response.Result;
import com.cjh.common.service.SpecificationsService;
import com.cjh.common.mapper.SpecificationsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 *
 */
@Service
public class SpecificationsServiceImpl extends ServiceImpl<SpecificationsMapper, Specifications>
    implements SpecificationsService{
    @Autowired
    SpecificationsMapper specificationsMapper;
    @Autowired
    TypeGroupMapper typeGroupMapper;
    @Autowired
    GroupValueMapper groupValueMapper;
    @Autowired
    CommodityMapper commodityMapper;
    @Override
    public Result getPagingSpecifications(SpecificationsPagingDTO specificationsPagingDTO) {
        if(specificationsPagingDTO.getSpecificationsId() != null){
            SpecificationsDTO specificationsById = specificationsMapper.getSpecificationsById(specificationsPagingDTO.getSpecificationsId());
            return new Result().setCode(200).setMessage("获取成功！").setData(specificationsById);
        }
        Integer size = specificationsPagingDTO.getSize();
        Integer start = (specificationsPagingDTO.getCurrent()-1)*size;
        PagingDTO<SpecificationsDTO> objectPagingDTO = new PagingDTO<>();
        objectPagingDTO.setRecords(specificationsMapper.getPagingSpecifications(start, size, specificationsPagingDTO.getName()));
        objectPagingDTO.setCurrent(specificationsPagingDTO.getCurrent());
        objectPagingDTO.setSize(size);
        objectPagingDTO.setTotal(specificationsMapper.getPagingSpecificationsTotal(specificationsPagingDTO.getName()));
        return new Result().setCode(200).setMessage("获取成功！").setData(objectPagingDTO);
    }
    @Override
    public Result getAllSpecifications() {
        return new Result().setCode(200).setMessage("获取成功！").setData(specificationsMapper.getAllSpecifications());
    }

    @Override
    public Result addSpecifications(SpecificationsDTO specificationsDTO) {
        Specifications specifications = new Specifications();
        BeanUtils.copyProperties(specificationsDTO,specifications);
        specificationsMapper.insert(specifications);
        TypeGroup typeGroup = new TypeGroup();
        typeGroup.setSpecificationsId(specifications.getSpecificationsId());
        for (TypeGroupDTO t:specificationsDTO.getTypeGroupDTOList()) {
            typeGroup.setTypeGroupName(t.getTypeGroupName());
            typeGroupMapper.insert(typeGroup);
            for (GroupValue g:t.getGroupValueList()){
                g.setTypeGroupId(typeGroup.getTypeGroupId());
                groupValueMapper.insert(g);
            }
        }
        return new Result().setCode(200).setMessage("新增成功！").setData(null);
    }

    public Integer deleteSpecificationsByDeleteSpecificationsDTO(DeleteSpecificationsDTO deleteSpecificationsDTO){
        LambdaQueryWrapper<Commodity> commodityLambdaQueryWrapper = Wrappers.lambdaQuery();
        commodityLambdaQueryWrapper.eq(Commodity::getSpecificationsId,deleteSpecificationsDTO.getSpecificationsId());
        if( commodityMapper.selectCount(commodityLambdaQueryWrapper) != 0){
            return 0;
        }
        groupValueMapper.deleteBatchIds(deleteSpecificationsDTO.getGroupValueIdList());
        typeGroupMapper.deleteBatchIds(deleteSpecificationsDTO.getTypeGroupIdList());
        specificationsMapper.deleteById(deleteSpecificationsDTO.getSpecificationsId());
        return 1;
    }

    @Override
    public Result deleteSpecifications(DeleteSpecificationsDTO deleteSpecificationsDTO) {
        if( deleteSpecificationsByDeleteSpecificationsDTO(deleteSpecificationsDTO) == 0){
            return new Result().setCode(201).setMessage("该规格有商品在作用，无法删除！");
        }else {
            return new Result().setCode(200).setMessage("删除成功！");
        }
    }

    @Override
    public Result deleteManySpecifications(List<DeleteSpecificationsDTO> deleteSpecificationsDTOList) {
        Integer allCount = deleteSpecificationsDTOList.size();
        Integer deleteCount = 0;
        for (DeleteSpecificationsDTO d:deleteSpecificationsDTOList){
            deleteCount += deleteSpecificationsByDeleteSpecificationsDTO(d);
        }
        Result result = new Result();
        if(deleteCount.equals(allCount)){
            result.setCode(200).setMessage("全部删除成功！");
        }else if(deleteCount == 0){
            result.setCode(201).setMessage("删除的规格全部有商品在作用，无法删除！");
        }else{
            result.setCode(201).setMessage("删除的规格部分有商品在作用无法删除，其余的删除成功！");
        }
        return result;
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateSpecifications(SpecificationsDTO specificationsDTO) {
        LambdaQueryWrapper<Commodity> commodityLambdaQueryWrapper = Wrappers.lambdaQuery();
        commodityLambdaQueryWrapper.eq(Commodity::getSpecificationsId,specificationsDTO.getSpecificationsId());
        if( commodityMapper.selectCount(commodityLambdaQueryWrapper) != 0){
            return new Result().setCode(201).setMessage("该规格有商品在作用，无法修改！");
        }
        Set<Integer> sqlTypeGroupIdList = typeGroupMapper.selectTypeGroupIdList(specificationsDTO.getSpecificationsId());
        for (Integer i:sqlTypeGroupIdList){
            groupValueMapper.delete(Wrappers.<GroupValue>lambdaQuery().eq(GroupValue::getTypeGroupId,i));
            typeGroupMapper.deleteById(i);
        }
        Specifications specifications = new Specifications();
        BeanUtils.copyProperties(specificationsDTO,specifications);
        specificationsMapper.updateById(specifications);
        for (TypeGroupDTO t:specificationsDTO.getTypeGroupDTOList()){
            TypeGroup typeGroup = new TypeGroup();
            typeGroup.setSpecificationsId(specifications.getSpecificationsId());
            typeGroup.setTypeGroupName(t.getTypeGroupName());
            typeGroupMapper.insert(typeGroup);
            for (GroupValue g:t.getGroupValueList()){
                GroupValue groupValue = new GroupValue();
                groupValue.setTypeGroupId(typeGroup.getTypeGroupId());
                groupValue.setGroupValueName(g.getGroupValueName());
                groupValueMapper.insert(groupValue);
            }
        }
        return new Result().setCode(200).setMessage("修改成功！");
    }
}




